#!/bin/bash
set -xe
PACKAGE_NAME="qt-examples"
VERSION="1.0.0"
ARCH="amd64"
MAINTAINER="Your Name <your-email@example.com>"
DESCRIPTION="Qt 示例程序（含独立 Qt 依赖）：基础窗口（main）和通知功能（main2）"
DEPENDS="libnotify4 (>= 0.7.0), libglib2.0-0 (>= 2.64.0), libgdk-pixbuf2.0-0 (>= 2.40.0), libx11-6, libxcb1"

BUILD_DIR="./build"
DEB_ROOT="${BUILD_DIR}/deb_root"
DEPLOY_DIR="${BUILD_DIR}/deploy"
MAIN_DEPLOY="${DEPLOY_DIR}/main"
MAIN2_DEPLOY="${DEPLOY_DIR}/main2"

rm -rf "${BUILD_DIR}"
mkdir -p "${BUILD_DIR}" "${DEPLOY_DIR}" "${MAIN_DEPLOY}" "${MAIN2_DEPLOY}"

# 编译程序
g++ main.cc -o "${BUILD_DIR}/main" \
  -std=c++17 \
  -I /usr/include/x86_64-linux-gnu/qt6/ \
  -L /lib/x86_64-linux-gnu/ \
  -l Qt6Widgets -l Qt6Core

g++ main2.cc -o "${BUILD_DIR}/main2" \
  -std=c++17 \
  -I /usr/include/x86_64-linux-gnu/qt6/ \
  -L /lib/x86_64-linux-gnu/ \
  -l Qt6Widgets -l Qt6Core \
  -I/usr/include/gdk-pixbuf-2.0 \
  -I/usr/include/glib-2.0 \
  -I/usr/lib/x86_64-linux-gnu/glib-2.0/include \
  -I/usr/include/libpng16 \
  -I/usr/include/x86_64-linux-gnu \
  -I/usr/include/libmount \
  -I/usr/include/blkid \
  -pthread -lnotify -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0

# 检查工具
if ! command -v linuxdeploy &> /dev/null; then
  echo "错误：未找到 linuxdeploy，请先安装"
  exit 1
fi
if ! command -v linuxdeploy-plugin-qt &> /dev/null; then
  echo "错误：未找到 linuxdeploy-plugin-qt，请先安装"
  exit 1
fi

# 核心修正：用 QT_PLUGIN_TYPES 强制收集 platforms 插件（linuxdeploy-plugin-qt 专属）
env QT_SELECT=6 QT_PLUGIN_TYPES=platforms \
  linuxdeploy --plugin qt -e "${BUILD_DIR}/main" --appdir "${MAIN_DEPLOY}"

env QT_SELECT=6 QT_PLUGIN_TYPES=platforms \
  linuxdeploy --plugin qt -e "${BUILD_DIR}/main2" --appdir "${MAIN2_DEPLOY}"

# 创建目录结构
mkdir -p "${DEB_ROOT}/usr/lib/qt-examples/main" \
         "${DEB_ROOT}/usr/lib/qt-examples/main2" \
         "${DEB_ROOT}/usr/bin" \
         "${DEB_ROOT}/DEBIAN"

# 完整复制依赖（含 plugins 目录）
cp -r "${MAIN_DEPLOY}/usr/bin/"* "${DEB_ROOT}/usr/lib/qt-examples/main/"
cp -r "${MAIN_DEPLOY}/usr/lib/"* "${DEB_ROOT}/usr/lib/qt-examples/main/"

cp -r "${MAIN2_DEPLOY}/usr/bin/"* "${DEB_ROOT}/usr/lib/qt-examples/main2/"
cp -r "${MAIN2_DEPLOY}/usr/lib/"* "${DEB_ROOT}/usr/lib/qt-examples/main2/"

# 启动脚本（自动适配插件路径）
cat > "${DEB_ROOT}/usr/bin/main" << EOF
#!/bin/bash
# 检测插件目录（兼容不同部署结构）
PLUGIN_DIR="/usr/lib/qt-examples/main/plugins"
if [ -d "/usr/lib/qt-examples/main/x86_64-linux-gnu/qt6/plugins" ]; then
  PLUGIN_DIR="/usr/lib/qt-examples/main/x86_64-linux-gnu/qt6/plugins"
fi
export LD_LIBRARY_PATH="/usr/lib/qt-examples/main:\$LD_LIBRARY_PATH"
export QT_PLUGIN_PATH="\${PLUGIN_DIR}:\$QT_PLUGIN_PATH"
exec /usr/lib/qt-examples/main/main "\$@"
EOF

cat > "${DEB_ROOT}/usr/bin/main2" << EOF
#!/bin/bash
PLUGIN_DIR="/usr/lib/qt-examples/main2/plugins"
if [ -d "/usr/lib/qt-examples/main2/x86_64-linux-gnu/qt6/plugins" ]; then
  PLUGIN_DIR="/usr/lib/qt-examples/main2/x86_64-linux-gnu/qt6/plugins"
fi
export LD_LIBRARY_PATH="/usr/lib/qt-examples/main2:\$LD_LIBRARY_PATH"
export QT_PLUGIN_PATH="\${PLUGIN_DIR}:\$QT_PLUGIN_PATH"
exec /usr/lib/qt-examples/main2/main2 "\$@"
EOF

# 权限设置
chmod 755 "${DEB_ROOT}/usr/lib/qt-examples/main/main"
chmod 755 "${DEB_ROOT}/usr/lib/qt-examples/main2/main2"
chmod 755 "${DEB_ROOT}/usr/bin/main" "${DEB_ROOT}/usr/bin/main2"

# 控制文件
cat > "${DEB_ROOT}/DEBIAN/control" << EOF
Package: ${PACKAGE_NAME}
Version: ${VERSION}
Architecture: ${ARCH}
Maintainer: ${MAINTAINER}
Description: ${DESCRIPTION}
Depends: ${DEPENDS}
Section: utils
Priority: optional
EOF
chmod 644 "${DEB_ROOT}/DEBIAN/control"

# 打包
dpkg-deb -b -Zgzip "${DEB_ROOT}" "${BUILD_DIR}/${PACKAGE_NAME}_${VERSION}_${ARCH}.deb"
echo "打包完成！生成的独立分发 Debian 包：${BUILD_DIR}/${PACKAGE_NAME}_${VERSION}_${ARCH}.deb"